﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace FluentHtmlHelpers.Controllers
{
    public class CourseController : ApiController
    {

        private static IList<Course> courses = new List<Course>()
            {
                new Course(){ Id = 0,Title="Course 0"},
                  new Course(){ Id = 1,Title="Course 1"}

            };

        // GET api/default1
        public IEnumerable<Course> Get()
        {
            return courses;
        }

        // GET api/default1/5
        public Course Get(int id)
        {
            var course = courses.FirstOrDefault(t => t.Id == id);
            return course;
        }

        // POST api/default1
        public void Post([FromBody]Course course)
        {
            var courseOriginal = courses.FirstOrDefault(t => t.Id == course.Id);
            if (courseOriginal == null)
            {
                courses.Add(course);
            }
        }

        // PUT api/default1/5
        public void Put(int id, [FromBody]Course course)
        {
            var courseOriginal = courses.FirstOrDefault(t => t.Id == course.Id);
            if (courseOriginal != null)
            {
                courseOriginal.Title = course.Title;
            }

        }

        // DELETE api/default1/5
        public void Delete(int id)
        {
            var courseOriginal = courses.FirstOrDefault(t => t.Id == id);
            if (courseOriginal != null)
            {
                courses.Remove(courseOriginal);
            }
        }
    }

    public class Course
    {
        public int Id { get; set; }
        public string Title { get; set; }
    }
}
